<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\model\AdvCount;
use app\common\model\AdvHtml;
use app\common\model\AdvLists;
use think\Db;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }

    public function template($link = '')
    {
        if ($link == '') {
            $this->error('链接已禁用', '');
        }
        $link = AdvLists::getByLink($link);
        if (!$link) {
            $this->error('链接已禁用', '');
        }
        if ($link['status'] != 1) {
            $this->error('链接已暂停使用', '');
        }
        $link['desc'] = mb_substr($link['desc'],0,50)."...";
        $this->assign('info', $link);
        $module = AdvHtml::get($link['adv_html_id']);
        if (!$module) {
            $this->error("链接模板不可用", '');
        }
        if($module['status'] != 1){
            $this->error('模板已停用，请更换其他模板','');
        }
        // 写访问日志
        $ip = $this->request->ip();
        $adLog = AdvCount::log($link['id'], $ip);
        $this->assign('adlog',$adLog);
        return $this->view->fetch($module['url']);
    }

    /**
     * 跳转前在做一次处理
     * @param string $ids 预处理的链接id
     * @param string $url 预跳转的链接
     */
    public function toTarget($ids = '', $url = '')
    {
        $isFullUrl = preg_match("/(http:\/\/|https:\/\/)/",$url);
        if(!$isFullUrl){
            $url = "http://".$url;
        }
        $this->redirect($url);
    }
}
